package com.htime.controller;

import com.htime.domain.City;
import com.htime.service.CityService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by HTime on 2017/3/3.
 */
@RestController
public class CityResController {

    @Autowired
    private CityService cityService;

    @ApiOperation(value="获取城市详细信息", notes="根据城市ID来获取城市详细信息")
    @ApiImplicitParam(name = "id", value = "城市ID", required = true, dataType = "Long")
    @RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
    public City findOneCity(@PathVariable("id") Long id) {
        return cityService.findCityById(id);
    }

    @ApiOperation(value="获取所有城市", notes="获取所有城市信息")
    @RequestMapping(value = "/api/city", method = RequestMethod.GET)
    public List<City> findAllCity() {
        return cityService.findAllCity();
    }

    @ApiOperation(value="创建城市", notes="根据City实体创建城市")
    @ApiImplicitParam(name = "city", value = "城市详细实体city", required = true, dataType = "City")
    @RequestMapping(value = "/api/city", method = RequestMethod.POST)
    public void createCity(@RequestBody City city) {
        cityService.saveCity(city);
    }

    @ApiOperation(value="更新城市", notes="根据传过来的City实体对是实体中的City ID进行更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "城市ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "city", value = "城市详细实体city", required = true, dataType = "City")
    })
    @RequestMapping(value = "/api/city/{id}", method = RequestMethod.PUT)
    public void modifyCity(@PathVariable Long id, @RequestBody City city) {
        City c = cityService.findCityById(id);
        c.setCityName(city.getCityName());
        c.setDescription(city.getDescription());
        c.setProvinceId(city.getProvinceId());
        cityService.updateCity(c);
    }

    @ApiOperation(value="删除城市", notes="根据指定的城市ID来删除城市")
    @ApiImplicitParam(name = "id", value = "城市ID", required = true, dataType = "Long")
    @RequestMapping(value = "/api/city/{id}", method = RequestMethod.DELETE)
    public void deleteCity(@PathVariable("id") Long id) {
        cityService.deleteCity(id);
    }

}
